/*
 * main.js
 * 监测预警主页
 */
;
(function(call) {
	var main = {
		init: function() {
			this.bindEvt();
			//如果是外链，隐藏tab
			if(this.queryParam('isOuterLink')){
				$('.cafe-navbar').hide();
				$(".cafe-container").addClass("outer");
			}
		},
		bindEvt: function() {
			this.handleTabClick();
			var that = this;
			$('ul.cafe-nav').undelegate('click').delegate('li', 'click', function(e){
				e.stopPropagation();
				if ($(this).data('channel') != '1') {
					$('.aui_state_focus').remove();
				}
				$(this).addClass('active').siblings().removeClass('active');
				that._ajaxHtml($(this));
				that.getUserInfo();
			})
		},
		handleTabClick: function() {
			let userId = this.queryParam('userId');
			if (userId === '111') {
				$('.cafe-nav li[channel="4"]').addClass('active').siblings().removeClass('active');
				$('.cafe-nav li[channel="4"]').prependTo($('.cafe-nav'));
			} else {
				$('.cafe-nav li[channel="1"]').addClass('active').siblings().removeClass('active');
				$('.cafe-nav li[channel="1"]').prependTo($('.cafe-nav'));
			}
		},
		getUserInfo: function() {
			var userId = this.queryParam('userId') || '10003';
            $("#userInfo").data('isMatchuser',userId == "00002"?true:false);
			$.getJSON(api.internal.common.getUserInfo, { userId: userId }, function (res) {
				if (!res.isSuccess) return;
				var userInfo = res.data;
				$("#userInfo").data('info', userInfo);
				var href = $('.cafe-nav li[channel="2"]').data('href');
				href = href.replace('#', userId);
				$('.cafe-nav li[channel="2"]').data('href', href);

				let href4 = $('.cafe-nav li[channel="4"]').data('href');
				href4 = href4.replace('$', userId);
				$('.cafe-nav li[channel="4"]').data('href', href4);
			});
			// let res = {"isSuccess":true,"message":null,"code":"200","data":{"roles":[{"iid":"2","roleId":"1","roleName":"超级管理员"}],"posts":[],"roleIds":["1"],"postIds":[],"userAreaCode":"12","smuser":{"iid":"2","loginName":"admin","name":"admin","pwd":"Qxt@2021ww","createTime":"2019-08-28 16:21:37","orgId":"1","mobile":"153","email":"153","forecasterId":"10001","enable":1.0}}};
			// if (!res.isSuccess) return;
			// var userInfo = res.data;
			// $("#userInfo").data('info', userInfo);
			// var href = $('.cafe-nav li[channel="2"]').data('href');
			// href = href.replace('#', userId);
			// $('.cafe-nav li[channel="2"]').data('href', href);

			// let href4 = $('.cafe-nav li[channel="4"]').data('href');
			// href4 = href4.replace('$', userId);
			// $('.cafe-nav li[channel="4"]').data('href', href4);
		},
		queryParam: function (val) {
            var uri = window.location.search;
            var re = new RegExp("" + val + "=([^&?]*)", "ig");
            return ((uri.match(re)) ? (uri.match(re)[0].substr(val.length + 1)) : null);
        },
		//请求html
		_ajaxHtml: function($nv){
			//匹配外链地址表达式
			var reghttp = /^http:*|^https:*/;
			var $container = $("div.cafe-container"),
				target = $nv.data('target'),
				href = $nv.data('href');
			$container.empty();
			if($("iframe#loader-wrap").length > 0) $("iframe#loader-wrap").remove();
			if(target != '_self' && href){
    			if(href.length > 1  && reghttp.test(href)){
    				if(target === '_blank') {
    					win.open(href);
    				} else if(target === '_frame'){
						
    					var $iframe = $('<iframe>').attr({
    						id : "loader-wrap",
    						name : "loader-wrap",
    						src : href,
    						frameborder : 0,
    						scrolling : "no",
    						initMethod : "none"
    					}).css({
    						width : "100%",
    						height : "100%",
    						position: "absolute",
    						background : "#FFF"
    					});
    					$container.append($iframe);
						/**setTimeout(()=>{
							document.getElementById('loader-wrap').contentWindow.postMessage({ message: CafeJS.rings }, '*');
						},1000)*/
						
    				}
    			}
    			return;
			}
			var ajaxurl = $nv.data('href');
			$.ajax({ 
    			url: ajaxurl, 
    			type: "GET",
    			dataType: "html",
    			crossDomain: true,
    			async: false,
    			context: $("div.cafe-container"), 
    			success: function(rest, textStatus, jqXHR){
    				if(jqXHR.status == 200 && textStatus == 'success'){
    					$(this).html(rest); 
    				}
    			},
    			error: function (XHR, textStatus, errorThrown) {console.log(errorThrown);},
    			complete: function(XHR, TS){}
    		});
		},
	};

	if(typeof call === 'function') {
		call(main);
	}

})(function(main) {
	main.init();
	$('ul.cafe-nav li.active').trigger('click');
	window.alert = function (info) {
		$.dialogMaster({
			"type": 'tip',
			"info": info
		});
	}
});